package com.eastfair.venueservice.service.impl;

//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.annotation.annotation.cache.Cache;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.dao.WorkOrderDrawingMapper;
import com.eastfair.venueservice.dto.WorkOrderDrawingDTO;
import com.eastfair.venueservice.entity.WorkOrderDrawing;
import com.eastfair.venueservice.service.WorkOrderDrawingService;
import com.eastfair.venueservice.vo.WorkOrderDrawingVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* <p>
    * 业务实现类
    * 工单图纸
    * </p>
*
* @author linan
* @date 2022-10-14
*/
@Slf4j
@Service
//@DS("#thread.tenant")
    public class WorkOrderDrawingServiceImpl extends SuperServiceImpl<WorkOrderDrawingMapper, WorkOrderDrawing> implements WorkOrderDrawingService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
    */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<WorkOrderDrawing> modelList) {
    modelList.stream().forEach(s -> {
    s.setIsDeleted(BusinessConstant.YES);
    });
    // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
    return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(WorkOrderDrawing model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    /**
    * 通用查询
    *
    * @param modelDTO DTO
    * @param ids 主键id集合
    * @return list
    */
    @Override
    public List<WorkOrderDrawing> list(WorkOrderDrawingDTO modelDTO, List<Long> ids) {
        return ((WorkOrderDrawingService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids 主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
       magic = @Magic(
       key = "{"+
       "{'isEnabled', #args[0]?.isEnabled},"+
       "{'workOrderNo', #args[0]?.workOrderNo},"+
       "{'spaceCode', #args[0]?.spaceCode},"+
       "{'spaceName', #args[0]?.spaceName},"+
       "{'siteCode', #args[0]?.siteCode},"+
       "{'siteName', #args[0]?.siteName},"+
       "{'picUrl', #args[0]?.picUrl},"+
       "{'picName', #args[0]?.picName},"+
       "{'picSource', #args[0]?.picSource},"+
        "{'id', #args[1]}"+
        "}"))
    public List<WorkOrderDrawing> listOfCache(WorkOrderDrawingDTO modelDTO, List<Long> ids) {
       //查询
       QueryWrapper<WorkOrderDrawing> queryWrapper = new QueryWrapper<>();
       if (modelDTO != null) {
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getWorkOrderNo()),WorkOrderDrawing::getWorkOrderNo,modelDTO.getWorkOrderNo());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getSpaceCode()),WorkOrderDrawing::getSpaceCode,modelDTO.getSpaceCode());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getSpaceName()),WorkOrderDrawing::getSpaceName,modelDTO.getSpaceName());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getSiteCode()),WorkOrderDrawing::getSiteCode,modelDTO.getSiteCode());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getSiteName()),WorkOrderDrawing::getSiteName,modelDTO.getSiteName());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getPicUrl()),WorkOrderDrawing::getPicUrl,modelDTO.getPicUrl());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getPicName()),WorkOrderDrawing::getPicName,modelDTO.getPicName());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getPicSource()),WorkOrderDrawing::getPicSource,modelDTO.getPicSource());
            }
            if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(WorkOrderDrawing::getId, ids);
            }
            queryWrapper.lambda().eq(WorkOrderDrawing::getIsDeleted, BusinessConstant.DELETE_NO);
            List<WorkOrderDrawing> modelList = list(queryWrapper);
            return modelList;
    }


    /**
     * do转vo
     * @param model model
     * @return modelVo
     */
    @Override
    public WorkOrderDrawingVO doToVo(WorkOrderDrawing model) {
        WorkOrderDrawingVO modelVo = new WorkOrderDrawingVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }
    /**
     * do转vo
     * @param modelList modelList
     * @return list
     */
     @Override
     public List<WorkOrderDrawingVO> doToVos(List<WorkOrderDrawing> modelList) {
        if (modelList == null || modelList.isEmpty()) {
        return null;
        }
        List<WorkOrderDrawingVO> result = new ArrayList<>();
        for (WorkOrderDrawing model : modelList) {
           result.add(doToVo(model));
        }
        return result;
    }
}
